package kr.hkit.powerjava.chapter17;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JCheckBox;
import javax.swing.BoxLayout;

import java.awt.GridLayout;

import javax.swing.ButtonGroup;
import javax.swing.JLabel;

import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;

public class CheckBoxLabel extends JPanel implements ItemListener {
	private JPanel panelCheck;
	JCheckBox[] buttons;
	JLabel[] lblPicture;
	String[] fruits = { "Apple", "Grape", "Orange" };
	ImageIcon[] icon = new ImageIcon[3];

	public CheckBoxLabel() {

		initialize();
	}

	private void initialize() {
		setLayout(new GridLayout(0, 4, 0, 0));

		panelCheck = new JPanel();
		panelCheck.setLayout(new GridLayout(0, 1, 0, 0));

		buttons = new JCheckBox[3];
		lblPicture = new JLabel[3];
		add(panelCheck);

		for (int i = 0; i < 3; i++) {
			buttons[i] = new JCheckBox(fruits[i]);
			buttons[i].addItemListener(this);
			lblPicture[i] = new JLabel(fruits[i] + ".jpg");
			icon[i] = new ImageIcon(fruits[i] + ".jpg");
			panelCheck.add(buttons[i]);
			add(lblPicture[i]);
		}

	}

	@Override
	public void itemStateChanged(ItemEvent e) {
		Object src = e.getItemSelectable();
		for (int i = 0; i < 3; i++) {
			if (src == buttons[i]) {
				if (e.getStateChange() == ItemEvent.DESELECTED) {
					lblPicture[i].setIcon(null);
				} else {
					lblPicture[i].setIcon(icon[i]);
				}
			}
		}
	}

}
